<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="keywords" content="董哲朋,博客,杰森,工程师杰森,攻城狮杰森,程序员杰森,jason,jasoncoding,pdpeng.github.io,coder-jason,杰森,工程师,攻城狮,程序员,blog,杰森的博客,全栈杰森,Jason,全栈">
<meta name="description" content="董哲朋的个人技术博客，进军 BAT">
<meta name="baidu-site-verification" content="code-QreKZHvhxr" />
<meta name="google-site-verification" content="09pUELCUBq1DqCCF38Yh2tJ3Ym_AwCrftq7Nuj6RFKU" />
<meta name="baidu-site-verification" content="code-UgQbVCF7gr" />
<meta name="google-site-verification" content="2Z5MbRq2MYkpxhxZigsnWu_02ovzdGiRJNOzHc_yq58" />
<meta name="sogou_site_verification" content="kR3XmMWqhc" />
<meta name="viewport" content="width=device-width">
<meta name="referrer" content="no-referrer" /> <!--临时解决防盗链-->
<meta name="theme-color" content="#222" media="(prefers-color-scheme: light)">
<meta name="theme-color" content="#222" media="(prefers-color-scheme: dark)"><meta name="generator" content="Hexo 6.3.0">

<link rel="preconnect" href="null//null" crossorigin>
<link rel="preconnect" href="https://cdn.jsdelivr.net" crossorigin>
  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16.png">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">
  <meta name="google-site-verification" content="yRcH84LYotQhqYFJcwA5UzGRX_E_HGt7EWyA1hBqqWU">
  <meta name="msvalidate.01" content="yRcH84LYotQhqYFJcwA5UzGRX_E_HGt7EWyA1hBqqWU">
  <meta name="yandex-verification" content="8db608d56597d365">
  <meta name="baidu-site-verification" content="code-SSriojapxO">

<link rel="stylesheet" href="/css/main.css">



<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free@6.1.1/css/all.min.css" integrity="sha256-DfWjNxDkM94fVBWx1H5BMMp0Zq7luBlV8QRcSES7s+0=" crossorigin="anonymous">
  <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/animate.css@3.1.1/animate.min.css" integrity="sha256-PR7ttpcvz8qrF57fur/yAx1qXMFJeJFiA6pSzWi0OIE=" crossorigin="anonymous">
  <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@3.5.7/dist/jquery.fancybox.min.css" integrity="sha256-Vzbj7sDDS/woiFS3uNKo8eIuni59rjyNGtXfstRzStA=" crossorigin="anonymous">

<script class="next-config" data-name="main" type="application/json">{"hostname":"pdpeng.github.io","root":"/","images":"/images","scheme":"Mist","darkmode":true,"version":"8.11.1","exturl":true,"sidebar":{"position":"left","display":"post","padding":18,"offset":12},"copycode":true,"bookmark":{"enable":false,"color":"#222","save":"manual"},"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":"utterances","storage":true,"lazyload":true,"nav":null,"activeClass":"utterances"},"stickytabs":false,"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"fadeInDown","post_body":"fadeInDown","coll_header":"fadeInLeft","sidebar":"fadeInUp"}},"prism":false,"i18n":{"placeholder":"搜索...","empty":"没有找到任何搜索结果：${query}","hits_time":"找到 ${hits} 个搜索结果（用时 ${time} 毫秒）","hits":"找到 ${hits} 个搜索结果"},"path":"/search.xml","localsearch":{"enable":true,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false}}</script><script src="/js/config.js"></script>



<link rel="canonical" href="https://pdpeng.github.io/2021/08/04/java-basics/">



<script class="next-config" data-name="page" type="application/json">{"sidebar":"","isHome":false,"isPost":true,"lang":"zh-CN","comments":true,"permalink":"https://pdpeng.github.io/2021/08/04/java-basics/","path":"2021/08/04/java-basics/","title":"Java 基础 ——入坑必读"}</script>

<script class="next-config" data-name="calendar" type="application/json">""</script>
<title>Java 基础 ——入坑必读 | 攻城狮杰森</title>
  
    <script async src="https://www.googletagmanager.com/gtag/js?id=G-5S6WNTLJG7"></script>
  <script class="next-config" data-name="google_analytics" type="application/json">{"tracking_id":"G-5S6WNTLJG7","only_pageview":false}</script>
  <script src="/js/third-party/analytics/google-analytics.js"></script>

  <script src="/js/third-party/analytics/baidu-analytics.js"></script>
  <script async src="https://hm.baidu.com/hm.js?11b3816992997637c6abc9c18c3eb3e5"></script>

  <script async src="//assets.growingio.com/2.1/gio.js"></script>
  <script class="next-config" data-name="growingio_analytics" type="application/json">"88ac8d4f08101068"</script>
  <script src="/js/third-party/analytics/growingio.js"></script>



  <noscript>
    <link rel="stylesheet" href="/css/noscript.css">
  </noscript>
<link rel="alternate" href="/atom.xml" title="攻城狮杰森" type="application/atom+xml">
</head>

<body itemscope itemtype="http://schema.org/WebPage" class="use-motion">
  <div class="headband"></div>

  <main class="main">
    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏" role="button">
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <i class="logo-line"></i>
      <p class="site-title">攻城狮杰森</p>
      <i class="logo-line"></i>
    </a>
      <p class="site-subtitle" itemprop="description">董哲朋的博客</p>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
        <i class="fa fa-search fa-fw fa-lg"></i>
    </div>
  </div>
</div>



<nav class="site-nav">
  <ul class="main-menu menu"><li class="menu-item menu-item-home"><a href="/" rel="section"><i class="fa fa-home fa-fw"></i>首页</a></li><li class="menu-item menu-item-categories"><a href="/categories/" rel="section"><i class="fa fa-th fa-fw"></i>分类<span class="badge">22</span></a></li><li class="menu-item menu-item-archives"><a href="/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>归档<span class="badge">105</span></a></li><li class="menu-item menu-item-tags"><a href="/tags/" rel="section"><i class="fa fa-tags fa-fw"></i>标签<span class="badge">28</span></a></li><li class="menu-item menu-item-about"><a href="/about/" rel="section"><i class="fa fa-user fa-fw"></i>关于</a></li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索
        </a>
      </li>
  </ul>
</nav>



  <div class="search-pop-overlay">
    <div class="popup search-popup"><div class="search-header">
  <span class="search-icon">
    <i class="fa fa-search"></i>
  </span>
  <div class="search-input-container">
    <input autocomplete="off" autocapitalize="off" maxlength="80"
           placeholder="搜索..." spellcheck="false"
           type="search" class="search-input">
  </div>
  <span class="popup-btn-close" role="button">
    <i class="fa fa-times-circle"></i>
  </span>
</div>
<div class="search-result-container no-result">
  <div class="search-result-icon">
    <i class="fa fa-spinner fa-pulse fa-5x"></i>
  </div>
</div>

    </div>
  </div>

</div>
        
  
  <div class="toggle sidebar-toggle" role="button">
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
  </div>

  <aside class="sidebar">

    <div class="sidebar-inner sidebar-nav-active sidebar-toc-active">
      <ul class="sidebar-nav">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <div class="sidebar-panel-container">
        <!--noindex-->
        <div class="post-toc-wrap sidebar-panel">
            <div class="post-toc animated"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#%E5%8E%86%E5%8F%B2%E6%B2%BF%E9%9D%A9"><span class="nav-number">1.</span> <span class="nav-text">历史沿革</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#Java-%E8%AF%AD%E8%A8%80%E5%9F%BA%E7%A1%80"><span class="nav-number">2.</span> <span class="nav-text">Java 语言基础</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#1-1-%E5%9F%BA%E6%9C%AC%E5%85%83%E7%B4%A0"><span class="nav-number">2.1.</span> <span class="nav-text">1.1 基本元素</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#1-1-1-%E6%A0%87%E8%AF%86%E7%AC%A6"><span class="nav-number">2.1.1.</span> <span class="nav-text">1.1.1 标识符</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#1-1-2-%E5%85%B3%E9%94%AE%E5%AD%97"><span class="nav-number">2.1.2.</span> <span class="nav-text">1.1.2 关键字</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#1-1-3-%E4%BF%9D%E7%95%99%E5%AD%97"><span class="nav-number">2.1.3.</span> <span class="nav-text">1.1.3 保留字</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#1-1-4-%E5%88%86%E9%9A%94%E7%AC%A6"><span class="nav-number">2.1.4.</span> <span class="nav-text">1.1.4 分隔符</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#1-2-%E6%95%B0%E6%8D%AE%E7%B1%BB%E5%9E%8B"><span class="nav-number">2.2.</span> <span class="nav-text">1.2 数据类型</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#1-2-1-%E5%9F%BA%E6%9C%AC%E6%95%B0%E6%8D%AE%E7%B1%BB%E5%9E%8B"><span class="nav-number">2.2.1.</span> <span class="nav-text">1.2.1 基本数据类型</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#1-2-2-%E5%8F%98%E9%87%8F"><span class="nav-number">2.2.2.</span> <span class="nav-text">1.2.2 变量</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#%E5%91%BD%E5%90%8D"><span class="nav-number">2.2.2.1.</span> <span class="nav-text">命名</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#%E5%A3%B0%E6%98%8E"><span class="nav-number">2.2.2.2.</span> <span class="nav-text">声明</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#%E5%88%9D%E5%A7%8B%E5%8C%96"><span class="nav-number">2.2.2.3.</span> <span class="nav-text">初始化</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#%E4%BD%9C%E7%94%A8%E5%9F%9F"><span class="nav-number">2.2.2.4.</span> <span class="nav-text">作用域</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#%E7%B1%BB%E5%9E%8B%E8%BD%AC%E6%8D%A2"><span class="nav-number">2.2.2.5.</span> <span class="nav-text">类型转换</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#1-2-3-%E5%B8%B8%E9%87%8F"><span class="nav-number">2.2.3.</span> <span class="nav-text">1.2.3 常量</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#%E6%95%B4%E5%9E%8B%E5%B8%B8%E9%87%8F"><span class="nav-number">2.2.3.1.</span> <span class="nav-text">整型常量</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#%E5%AE%9E%E5%9E%8B%E5%B8%B8%E9%87%8F"><span class="nav-number">2.2.3.2.</span> <span class="nav-text">实型常量</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#%E5%B8%83%E5%B0%94%E5%B8%B8%E9%87%8F"><span class="nav-number">2.2.3.3.</span> <span class="nav-text">布尔常量</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#%E5%AD%97%E7%AC%A6%E4%B8%B2%E5%B8%B8%E9%87%8F"><span class="nav-number">2.2.3.4.</span> <span class="nav-text">字符串常量</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#%E5%AD%97%E7%AC%A6%E5%B8%B8%E9%87%8F%E5%92%8Cnull%E5%B8%B8%E9%87%8F"><span class="nav-number">2.2.3.5.</span> <span class="nav-text">字符常量和null常量</span></a></li></ol></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#1-3-%E8%BF%90%E7%AE%97%E7%AC%A6%E4%B8%8E%E8%A1%A8%E8%BE%BE%E5%BC%8F"><span class="nav-number">2.3.</span> <span class="nav-text">1.3 运算符与表达式</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#1-3-1-%E7%AE%97%E6%9C%AF%E8%BF%90%E7%AE%97%E7%AC%A6"><span class="nav-number">2.3.1.</span> <span class="nav-text">1.3.1 算术运算符</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#1-3-2-%E9%80%BB%E8%BE%91%E8%BF%90%E7%AE%97%E7%AC%A6"><span class="nav-number">2.3.2.</span> <span class="nav-text">1.3.2 逻辑运算符</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#1-3-3-%E6%9D%A1%E4%BB%B6%E8%BF%90%E7%AE%97%E7%AC%A6"><span class="nav-number">2.3.3.</span> <span class="nav-text">1.3.3 条件运算符</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#1-3-4-%E4%BD%8D%E8%BF%90%E7%AE%97%E7%AC%A6"><span class="nav-number">2.3.4.</span> <span class="nav-text">1.3.4 位运算符</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#1-3-5-%E8%B5%8B%E5%80%BC%E8%BF%90%E7%AE%97%E7%AC%A6"><span class="nav-number">2.3.5.</span> <span class="nav-text">1.3.5 赋值运算符</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#1-3-6-%E8%A1%A8%E8%BE%BE%E5%BC%8F%E7%B1%BB%E5%9E%8B%E6%8F%90%E5%8D%87"><span class="nav-number">2.3.6.</span> <span class="nav-text">1.3.6 表达式类型提升</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#1-4-%E8%AF%AD%E5%8F%A5%E5%8F%8A%E5%85%B6%E6%8E%A7%E5%88%B6%E7%BB%93%E6%9E%84"><span class="nav-number">2.4.</span> <span class="nav-text">1.4 语句及其控制结构</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#1-4-1-Java%E8%AF%AD%E5%8F%A5"><span class="nav-number">2.4.1.</span> <span class="nav-text">1.4.1 Java语句</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#%E8%A1%A8%E8%BE%BE%E5%BC%8F%E8%AF%AD%E5%8F%A5"><span class="nav-number">2.4.1.1.</span> <span class="nav-text">表达式语句</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#%E5%A4%8D%E5%90%88%E8%AF%AD%E5%8F%A5"><span class="nav-number">2.4.1.2.</span> <span class="nav-text">复合语句</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#1-4-2-%E9%80%89%E6%8B%A9%E8%AF%AD%E5%8F%A5"><span class="nav-number">2.4.2.</span> <span class="nav-text">1.4.2 选择语句</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#if%C2%B7%C2%B7%C2%B7else%E8%AF%AD%E5%8F%A5"><span class="nav-number">2.4.2.1.</span> <span class="nav-text">if···else语句</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#switch%E8%AF%AD%E5%8F%A5"><span class="nav-number">2.4.2.2.</span> <span class="nav-text">switch语句</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#1-4-3-%E5%BE%AA%E7%8E%AF%E8%AF%AD%E5%8F%A5"><span class="nav-number">2.4.3.</span> <span class="nav-text">1.4.3 循环语句</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#for%E5%BE%AA%E7%8E%AF%E8%AF%AD%E5%8F%A5"><span class="nav-number">2.4.3.1.</span> <span class="nav-text">for循环语句</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#while%E5%BE%AA%E7%8E%AF%E8%AF%AD%E5%8F%A5"><span class="nav-number">2.4.3.2.</span> <span class="nav-text">while循环语句</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#beak%E8%AF%AD%E5%8F%A5"><span class="nav-number">2.4.3.3.</span> <span class="nav-text">beak语句</span></a></li></ol></li></ol></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E6%80%BB%E7%BB%93"><span class="nav-number">3.</span> <span class="nav-text">总结</span></a></li></ol></div>
        </div>
        <!--/noindex-->

        <div class="site-overview-wrap sidebar-panel">
          <div class="site-author site-overview-item animated" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <a href="/">
    <img class="site-author-image" itemprop="image" alt="董哲朋"
      src="/images/head_portrait.png">
    </a>
  <p class="site-author-name" itemprop="name">董哲朋</p>
  <div class="site-description" itemprop="description" style="padding-bottom: 8px">A blacksmith must be strong</div>

<html>
<head>
    <style>
        .chat-button div {
            display: block;
            margin-top: 5px;
            background-color: transparent;
            position: relative;
            font-family: 'Lato', "PingFang SC", "Microsoft YaHei", sans-serif;
            font-size: 14px;
            line-height: 2;
        }

        .feed-link a {
            display: inline-block;
            padding: 0 15px;
            color: #fc6423;
            border: 2px solid #fc6423;
            border-radius: 6px;
            text-decoration: none;
        }

        .feed-link a:hover {
            background-color: #fc6423;
            border-bottom-color: #fc6423;
            color: #fff;
        }
    </style>
</head>

<body class="chat-button">
    <div class="feed-link" title="Q&A → Blog-Jason/community">
        <a target="_blank" rel="noopener" href="https://gitter.im/Blog-Jason/community">
            Q&A
        </a>
    </div>
</body>
</html>

</div>
<div class="site-state-wrap site-overview-item animated">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
        <a href="/archives/">
          <span class="site-state-item-count">105</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
          <a href="/categories/">
        <span class="site-state-item-count">22</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
          <a href="/tags/">
        <span class="site-state-item-count">28</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>
  <div class="links-of-author site-overview-item animated">
      <span class="links-of-author-item">
        <span class="exturl" data-url="aHR0cHM6Ly93d3cubGlua2VkaW4uY29tL2luL2Rvbmd6aGVwZW5n" title="Linkedin → https:&#x2F;&#x2F;www.linkedin.com&#x2F;in&#x2F;dongzhepeng"><i class="fab fa-linkedin fa-fw"></i>Linkedin</span>
      </span>
      <span class="links-of-author-item">
        <span class="exturl" data-url="aHR0cHM6Ly90d2l0dGVyLmNvbS9qYXNvbmk5OTY=" title="Twitter → https:&#x2F;&#x2F;twitter.com&#x2F;jasoni996"><i class="fab fa-twitter fa-fw"></i>Twitter</span>
      </span>
      <span class="links-of-author-item">
        <span class="exturl" data-url="bWFpbHRvOmR6aGVwZW5nQGdtYWlsLmNvbQ==" title="E-Mail → mailto:dzhepeng@gmail.com"><i class="fa fa-envelope fa-fw"></i>E-Mail</span>
      </span>
      <span class="links-of-author-item">
        <span class="exturl" data-url="aHR0cHM6Ly9zdGFja292ZXJmbG93LmNvbS91c2Vycy8xNjY0ODc1NA==" title="S.O. → https:&#x2F;&#x2F;stackoverflow.com&#x2F;users&#x2F;16648754"><i class="fab fa-stack-overflow fa-fw"></i>S.O.</span>
      </span>
      <span class="links-of-author-item">
        <a href="/atom.xml" title="RSS → &#x2F;atom.xml"><i class="fa fa-rss fa-fw"></i>RSS</a>
      </span>
      <span class="links-of-author-item">
        <span class="exturl" data-url="aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L20wXzUxMjY5OTYx" title="CSDN → https:&#x2F;&#x2F;blog.csdn.net&#x2F;m0_51269961"><i class="fa-solid fa-c fa-fw"></i>CSDN</span>
      </span>
  </div>
  <div class="cc-license site-overview-item animated" itemprop="license">
    <span class="exturl cc-opacity" data-url="aHR0cHM6Ly9jcmVhdGl2ZWNvbW1vbnMub3JnL2xpY2Vuc2VzL2J5LW5jLXNhLzQuMC8="><img src="https://cdn.jsdelivr.net/npm/@creativecommons/vocabulary@2020.11.3/assets/license_badges/small/by_nc_sa.svg" alt="Creative Commons"></span>
  </div>



        </div>
      </div>

      
      <script type="text/javascript" charset="utf-8" src="/js/tagcloud.js"></script>
      <script type="text/javascript" charset="utf-8" src="/js/tagcanvas.js"></script>
      <div class="widget-wrap">
          <div id="myCanvasContainer" class="widget tagcloud">
              <canvas width="250" height="250" id="resCanvas" style="width:100%">
                  <ul class="tag-list" itemprop="keywords"><li class="tag-list-item"><a class="tag-list-link" href="/tags/ACM/" rel="tag">ACM</a><span class="tag-list-count">32</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/C/" rel="tag">C++</a><span class="tag-list-count">4</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/CDN/" rel="tag">CDN</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/CPU/" rel="tag">CPU</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/DNS/" rel="tag">DNS</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/Git/" rel="tag">Git</a><span class="tag-list-count">9</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/HarmonyOS/" rel="tag">HarmonyOS</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/Hexo/" rel="tag">Hexo</a><span class="tag-list-count">2</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/IOS/" rel="tag">IOS</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/Java/" rel="tag">Java</a><span class="tag-list-count">9</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/Linux/" rel="tag">Linux</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/Markdown/" rel="tag">Markdown</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/MySQL/" rel="tag">MySQL</a><span class="tag-list-count">2</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/Python/" rel="tag">Python</a><span class="tag-list-count">4</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/RT-Thread/" rel="tag">RT-Thread</a><span class="tag-list-count">3</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/SQLServer/" rel="tag">SQLServer</a><span class="tag-list-count">9</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/TCP-IP/" rel="tag">TCP&#x2F;IP</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/Tomcat/" rel="tag">Tomcat</a><span class="tag-list-count">2</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/Vivado/" rel="tag">Vivado</a><span class="tag-list-count">2</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/%E4%BA%91%E5%AD%98%E5%82%A8/" rel="tag">云存储</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/%E5%89%8D%E7%AB%AF/" rel="tag">前端</a><span class="tag-list-count">6</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/%E6%9C%8D%E5%8A%A1%E5%99%A8/" rel="tag">服务器</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/%E7%A8%8B%E5%BA%8F%E4%BA%BA%E7%94%9F/" rel="tag">程序人生</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/%E7%BC%96%E7%A0%81/" rel="tag">编码</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/%E8%81%8C%E4%B8%9A%E8%AE%A4%E8%AF%81/" rel="tag">职业认证</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/%E8%A7%A3%E5%86%B3%E6%96%B9%E6%A1%88/" rel="tag">解决方案</a><span class="tag-list-count">6</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/%E8%A7%A3%E6%9E%90%E5%87%A0%E4%BD%95/" rel="tag">解析几何</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/%E8%AE%A1%E7%AE%97%E6%9C%BA%E8%A7%86%E8%A7%89/" rel="tag">计算机视觉</a><span class="tag-list-count">1</span></li></ul>
              </canvas>
          </div>
      </div>
      
        <div class="back-to-top animated" role="button" aria-label="返回顶部">
          <i class="fa fa-arrow-up"></i>
          <span>0%</span>
        </div>
    </div>
  </aside>
  <div class="sidebar-dimmer"></div>


    </header>

    
  <div class="reading-progress-bar"></div>

  <span class="exturl github-corner" data-url="aHR0cHM6Ly9naXRodWIuY29tL1BEUEVORw==" title="Follow Jason on GitHub" aria-label="Follow Jason on GitHub"><svg width="80" height="80" viewBox="0 0 250 250" aria-hidden="true"><path d="M0,0 L115,115 L130,115 L142,142 L250,250 L250,0 Z"></path><path d="M128.3,109.0 C113.8,99.7 119.0,89.6 119.0,89.6 C122.0,82.7 120.5,78.6 120.5,78.6 C119.2,72.0 123.4,76.3 123.4,76.3 C127.3,80.9 125.5,87.3 125.5,87.3 C122.9,97.6 130.6,101.9 134.4,103.2" fill="currentColor" style="transform-origin: 130px 106px;" class="octo-arm"></path><path d="M115.0,115.0 C114.9,115.1 118.7,116.5 119.8,115.4 L133.7,101.6 C136.9,99.2 139.9,98.4 142.2,98.6 C133.8,88.0 127.5,74.4 143.8,58.0 C148.5,53.4 154.0,51.2 159.7,51.0 C160.3,49.4 163.2,43.6 171.4,40.1 C171.4,40.1 176.1,42.5 178.8,56.2 C183.1,58.6 187.2,61.8 190.9,65.4 C194.5,69.0 197.7,73.2 200.1,77.6 C213.8,80.2 216.3,84.9 216.3,84.9 C212.7,93.1 206.9,96.0 205.4,96.6 C205.1,102.4 203.0,107.8 198.3,112.5 C181.9,128.9 168.3,122.5 157.7,114.1 C157.9,116.9 156.7,120.9 152.7,124.9 L141.0,136.5 C139.8,137.7 141.6,141.9 141.8,141.8 Z" fill="currentColor" class="octo-body"></path></svg></span>

<noscript>
  <div class="noscript-warning">Theme NexT works best with JavaScript enabled</div>
</noscript>


    <div class="main-inner post posts-expand">


  


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="https://pdpeng.github.io/2021/08/04/java-basics/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/head_portrait.png">
      <meta itemprop="name" content="董哲朋">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="攻城狮杰森">
      <meta itemprop="description" content="A blacksmith must be strong">
    </span>

    <span hidden itemprop="post" itemscope itemtype="http://schema.org/CreativeWork">
      <meta itemprop="name" content="Java 基础 ——入坑必读 | 攻城狮杰森">
      <meta itemprop="description" content="">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          Java 基础 ——入坑必读
        </h1>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">发表于</span>

      <time title="创建时间：2021-08-04 17:45:24" itemprop="dateCreated datePublished" datetime="2021-08-04T17:45:24+08:00">2021-08-04</time>
    </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-folder"></i>
      </span>
      <span class="post-meta-item-text">分类于</span>
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/Java/" itemprop="url" rel="index"><span itemprop="name">Java</span></a>
        </span>
    </span>

  
    <span id="/2021/08/04/java-basics/" class="post-meta-item leancloud_visitors" data-flag-title="Java 基础 ——入坑必读" title="阅读次数">
      <span class="post-meta-item-icon">
        <i class="far fa-eye"></i>
      </span>
      <span class="post-meta-item-text">阅读次数：</span>
      <span class="leancloud-visitors-count"></span>
    </span>
    <span class="post-meta-break"></span>
    <span class="post-meta-item" title="本文字数">
      <span class="post-meta-item-icon">
        <i class="far fa-file-word"></i>
      </span>
      <span class="post-meta-item-text">本文字数：</span>
      <span>11k</span>
    </span>
    <span class="post-meta-item" title="阅读时长">
      <span class="post-meta-item-icon">
        <i class="far fa-clock"></i>
      </span>
      <span class="post-meta-item-text">阅读时长 &asymp;</span>
      <span>10 分钟</span>
    </span>
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">
        <h1 id="历史沿革"><a href="#历史沿革" class="headerlink" title="历史沿革"></a>历史沿革</h1><p>Java是印度尼西亚“爪哇岛”的英文名，因气候环境，地理位置等因素盛产咖啡而闻名。Java语言中的许多命名都与其有关：如<span class="exturl" data-url="aHR0cHM6Ly9iYWlrZS5iYWlkdS5jb20vaXRlbS9KYXZhQmVhbnMvNTE1NTYxMg==">JavaBeans<i class="fa fa-external-link-alt"></i></span>。所以Sun和Java的Logo是个非常形象的图标—冒热气儿的咖啡</p>
<p>早期的Java只是<span class="exturl" data-url="aHR0cHM6Ly9iYWlrZS5iYWlkdS5jb20vaXRlbS9TdW4lMjBNaWNyb3N5c3RlbXM=">Sun MicroSystems<i class="fa fa-external-link-alt"></i></span>公司在1990年12月开始研究的一个内部项目。Sun公司一位名为帕特里克·诺顿的工程师因为其公司难于操作的C++&#x2F;C语言编译器而决定改用<span class="exturl" data-url="aHR0cHM6Ly9iYWlrZS5iYWlkdS5jb20vaXRlbS9uZXh0LzE3MTk0OTY=">NeXT<i class="fa fa-external-link-alt"></i></span>，同时他也获得了研究公司的一个叫做“Stealth计划”的项目的机会</p>
<p>“Stealth计划”后来改名为“Green计划”，詹姆斯·高斯林和Mike Sheridan也加入了帕特里克的工作小组。根据有限资金，乔伊决定开发一种集C语言和Mesa语言大成的新语言，在一份报告上，乔伊把它叫做“未来”。他将要创造出一种全新的语言，被他命名为“Oak”，以办公室外的橡树命名</p>
<span id="more"></span>
<h1 id="Java-语言基础"><a href="#Java-语言基础" class="headerlink" title="Java 语言基础"></a>Java 语言基础</h1><h2 id="1-1-基本元素"><a href="#1-1-基本元素" class="headerlink" title="1.1 基本元素"></a>1.1 基本元素</h2><h3 id="1-1-1-标识符"><a href="#1-1-1-标识符" class="headerlink" title="1.1.1 标识符"></a>1.1.1 标识符</h3><p>（1）标识符包含类名、变量名、常量名、方法名…；</p>
<p>（2）命名规则：字母、下划线、美元符号开头，后可接此三种符号或0-9的数字，注意不要和关键字   重合；</p>
<p>（3）命名“约定”：变量名、方法名首单词小写，其余单词仅首字母大写。类名、接口名首字母大写</p>
<p>常量全都用大写字母、单词命名，下划线常用于常量名单词的分隔。Java严格区分字母大小写，例如aB和Ab是完全不同的合法标识符；</p>
<h3 id="1-1-2-关键字"><a href="#1-1-2-关键字" class="headerlink" title="1.1.2 关键字"></a>1.1.2 关键字</h3><image src="https://img-blog.csdnimg.cn/cd707ae0f1124001851f186e02f6afb1.png" width="90%">

<p>重点标记一些Java中特有的关键字：<br>（1)数据类型：<mark class="label ">boolean</mark>、byte、char、double、false、true、void、<mark class="label ">instanceof</mark>、float、int、long、new、<mark class="label ">null</mark>、short；</p>
<p>（2)语句：break、case、catch、continue、、return、<mark class="label ">super</mark>、switch、this、throw、try、while、default、do、else、<mark class="label ">finally</mark>、for、if；</p>
<p>（3)修饰：<mark class="label ">abstract</mark>、<mark class="label ">final</mark>、<mark class="label ">native</mark>、<mark class="label ">transient</mark>、volatile、private、protected、public、static、<mark class="label ">synchronized</mark>；</p>
<p>（4)方法、类、接口、包：<mark class="label ">interface</mark>、<mark class="label ">import</mark>、class、<mark class="label ">extends</mark>、 <mark class="label ">implements</mark>、<mark class="label ">package</mark>、throws；</p>
<h3 id="1-1-3-保留字"><a href="#1-1-3-保留字" class="headerlink" title="1.1.3 保留字"></a>1.1.3 保留字</h3><p>一些不能用做自定义标识符，但又有自己专门用途和意义的 “ 关键字 ” ；形象的来讲就是他们没有成为关键字的权利，但是有享受关键字的“待遇”，像是充值VIP一般，sizeof、goto、const，它们仨就是VIP中的成员。说不定随着语言的发展，慢慢它们也成为关键字了呢？</p>
<h3 id="1-1-4-分隔符"><a href="#1-1-4-分隔符" class="headerlink" title="1.1.4 分隔符"></a>1.1.4 分隔符</h3><p>像C&#x2F;C++中熟知的空白符（Tab、回车、空格、换行）、普通分隔符（点、分号、逗号……）、单行注释符 <code>// 注释内容</code> ，多行注释符 <code>/* 注释内容 */</code> 就不再详细展开来讲，这里我们主要了解Java中一些不一样的东西—Java文档注释格式，它的格式是<code>/ * *注释内容 * /</code></p>
<p>注意：这类注释只应用在声明之前，最为方便的是它可以利用<span class="exturl" data-url="aHR0cHM6Ly9iYWlrZS5iYWlkdS5jb20vaXRlbS9qZGsvMTAxMT9mcj1hbGFkZGlu">JDK<i class="fa fa-external-link-alt"></i></span>中提供的 javadoc 工具，根据注释内容自动生成HTML代码说明文档</p>
<h2 id="1-2-数据类型"><a href="#1-2-数据类型" class="headerlink" title="1.2 数据类型"></a>1.2 数据类型</h2><p>那么，何为数据类型或者说数据类型能说明什么呢?它存在的意义就是：表明变量、常量、表达式的性质。在进行运算时，只有符合“相同数据类型”这一条件，才可以继续进行。按照大类区分，Java中有简单、引用两种数据类型。分类如图所示</p>
<image src="https://img-blog.csdnimg.cn/e7d86e6e8cbb44468cfa424287fab354.PNG" width="50%">

<h3 id="1-2-1-基本数据类型"><a href="#1-2-1-基本数据类型" class="headerlink" title="1.2.1 基本数据类型"></a>1.2.1 基本数据类型</h3><p>在使用计算机进行运算时，我们都知道不同的数据类型在其中表示的数值范围、所占内存大小都不相同，下表给出了Java基本数据类型的具体参数</p>
<image src="https://img-blog.csdnimg.cn/89d6b81fb52a480b841e72551bd72de7.png" width="80%">

<p>表中，我们仍能对这些数据类型进行划分，比如byte、short、int、long、float、double属于数值型数据，char、boolean属于非数值型数据。Java因其基本数据类型拥有固定长度的特性，而促成了其基本数据类型优良的跨平台移植属性，数值类型的数据都与系统无关</p>
<h3 id="1-2-2-变量"><a href="#1-2-2-变量" class="headerlink" title="1.2.2 变量"></a>1.2.2 变量</h3><p>变量是程序中存储单元的标识符，它代表内存中的一块空间。在Java中，变量还具有类型、名称、值、作用域等属性</p>
<h4 id="命名"><a href="#命名" class="headerlink" title="命名"></a>命名</h4><p>遵循命名规则：</p>
<p>（1）长度不限，但系统仅承载前32个字符，变量名必须以小写字母开头，且首字符不能为数字；</p>
<p>（2）在同一作用域内，变量名必唯一且区分大小写；</p>
<p>（3）不能是关键字，布尔值或保留字（null)；</p>
<p>（4）若多个单词构成变量名，则单词需要连在一起写，且除第一个单词外，其余单词首字母需大写；</p>
<h4 id="声明"><a href="#声明" class="headerlink" title="声明"></a>声明</h4><p>两种声明格式：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"> <span class="type">int</span> a,b,c;</span><br><span class="line"> <span class="type">float</span> b1,i1;</span><br><span class="line"> <span class="type">double</span> c,c1=<span class="number">2d</span>;  <span class="comment">//声明变量同时初始化</span></span><br><span class="line">（<span class="number">1</span>）类型  变量名［变量名<span class="number">1</span>,···变量名n];</span><br><span class="line">（<span class="number">2</span>）类型  变量名［变量名<span class="number">1</span>,···交量名n]=初值，···;</span><br></pre></td></tr></table></figure>

<h4 id="初始化"><a href="#初始化" class="headerlink" title="初始化"></a>初始化</h4><p>变量声明后，使用赋值语句进行初始化：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">varName1 = [ varName2 = ] 表达式 ;</span><br><span class="line"><span class="type">int</span> Var1,var2;</span><br><span class="line">var1=var2=<span class="number">5</span>;</span><br></pre></td></tr></table></figure>

<p>允许同时给多个变量赋值：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">类型 varName1 = 表达式<span class="number">1</span> [ , varName2 = 表达式<span class="number">2</span> ] ;</span><br><span class="line"><span class="type">float</span> Var1=<span class="number">2.3f</span>,Var2=<span class="number">3.2f</span>;</span><br></pre></td></tr></table></figure>

<p>变量在声明的同时进行初始化：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">类型 变量名=表达式;</span><br><span class="line"><span class="type">double</span> variable=<span class="number">600d</span>;</span><br></pre></td></tr></table></figure>

<p>注意：除可以进行自动类型转换的数据外，变量的赋值需要“＝”号两边的数据类型一致</p>
<h4 id="作用域"><a href="#作用域" class="headerlink" title="作用域"></a>作用域</h4><p>变量要先定义后使用，变量定义所在的位置决定了变量的作用域，根据变量定义所处位置，其作用域可以分为：局部变量、类成员变量、方法参数变量、异常处理参数变量</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">TypeDefinition</span> &#123;</span><br><span class="line">    <span class="keyword">static</span> <span class="type">char</span> charVar=<span class="string">&#x27;\t&#x27;</span>; <span class="comment">//  类成员变量</span></span><br><span class="line">    <span class="keyword">static</span> <span class="keyword">final</span> <span class="type">float</span> floatVar=<span class="number">3.1415926f</span>; <span class="comment">//定义单精度浮点型常量floatVar       类成员变量</span></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span>&#123;</span><br><span class="line">        String stringVar=<span class="string">&quot;Java&quot;</span>;   <span class="comment">//在该语句块中有效       </span></span><br><span class="line">        System.out.println(<span class="string">&quot;类中定义:floatVar=&quot;</span>+floatVar+charVar+<span class="string">&quot;stringVar=&quot;</span>+stringVar);</span><br><span class="line">        System.out.println();  <span class="comment">//光标换行</span></span><br><span class="line">        show();</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">show</span><span class="params">()</span>&#123;</span><br><span class="line">        String stringVar=<span class="string">&quot;Test&quot;</span>;</span><br><span class="line">        System.out.println(<span class="string">&quot;类中定义:floatVar=&quot;</span>+floatVar);</span><br><span class="line">        System.out.println(<span class="string">&quot;方法中定义:stringVar=&quot;</span>+stringVar);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h4 id="类型转换"><a href="#类型转换" class="headerlink" title="类型转换"></a>类型转换</h4><p>当数值在不同的数据类型之间进行运算时，需要转换为统一类型来进行，不同数据类型之间有对应的转换规则。可以分为 “ 自动类型转换 ” 和 “ 强制类型转换 ” 两种</p>
<p>自动类型转换也称 “ 隐式类型转换 ” ，即编码是不需要程序员操作，系统会自动进行类型转换，一般遵循低精度可以自动转换到高精度的规则</p>
<p>但是，如果数据由高精度转换成低精度呢？那么我们就需要进行强制类型转换<br>强制转换的格式为：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">（数据类型）表达式 ;</span><br><span class="line"><span class="type">int</span> a;</span><br><span class="line"><span class="type">byte</span> c=<span class="type">byte</span>(i); <span class="comment">//int型数据a强制转换为byte型</span></span><br></pre></td></tr></table></figure>

<p>注意：根据变量所占位长的不同，在强制类型转换中可能发生数据丢失的现象，使用需谨慎。下面展示强制转换后数据丢失的例子：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">TypeConversion</span> &#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span>&#123;</span><br><span class="line">        <span class="type">int</span> intVar=<span class="number">0xff56</span>;</span><br><span class="line">        <span class="type">byte</span> byteVar=(<span class="type">byte</span>)intVar;<span class="comment">//数据强制类型转换 32位整数强制转换为8为字节数</span></span><br><span class="line">        System.out.println(<span class="string">&quot;intVar=&quot;</span>+Integer.toString(intVar,<span class="number">2</span>)+<span class="string">&quot;;&quot;</span>+intVar); <span class="comment">//转换为二进制,int型数据</span></span><br><span class="line">        System.out.println(<span class="string">&quot;byteVar=&quot;</span>+Integer.toString(byteVar,<span class="number">2</span>)+<span class="string">&quot;;&quot;</span>+byteVar);<span class="comment">//转换为二进制,字节型数据</span></span><br><span class="line">    &#125;</span><br><span class="line">&#125;      <span class="comment">//整型数据int有32位,而byte字节型数据只有8位,类型转换后数据丢失</span></span><br></pre></td></tr></table></figure>

<h3 id="1-2-3-常量"><a href="#1-2-3-常量" class="headerlink" title="1.2.3 常量"></a>1.2.3 常量</h3><p>常量是在程序中直接给出的一系类符号串，它的作用是为变量赋值或参与表达式的运算。可以是一个具体的数值或字符串。常量在程序执行过程中不可更改</p>
<p>常量标识符命名全部由大写字母组成，多个单词之间使用下划线进行连接，eg:Largest_Integer</p>
<h4 id="整型常量"><a href="#整型常量" class="headerlink" title="整型常量"></a>整型常量</h4><p>可以使计算机表示某个整数值，十进制、十六进制和八进制类型数据。八进制常量以 0 开头，后接 0 - 7 之间的数字（这里常常会设置一些考题，比如问：089 是什么数？千万别傻傻的写八进制数了，它是非法数）。十六进制常量以0x开头后跟多个 0 - 9之间的数字或 a - f （ A - F ）之间的字母</p>
<p>byte、short、int、long这四种类型是较为特殊的数据类型。byte 和 short类型主要应用于底层文件处理或在存储空间极其宝贵、又需要保存大型数组的特殊情况。long类型可表示数值较大的整数。int类型是最常用的，默认32位，若需要表示64位的long类型常量则在数字后加上大写字母 L 来区分</p>
<h4 id="实型常量"><a href="#实型常量" class="headerlink" title="实型常量"></a>实型常量</h4><p>两种表示形式：</p>
<p>（1）十进制数：由数字和小数点构成，且必须包含小数点；由于实数表示精度不同（单精度、双精度）。Java中默认实型常量为计算机中占64位的double类型，单精度数后面要加“f”或<br>“F”。双精度、多精度数后加“d”或“D”</p>
<p>（2）科学计数法：适用于数据过大或过小的情况，字母e或E前必须含有数字，后面的指数只能为整型数据</p>
<h4 id="布尔常量"><a href="#布尔常量" class="headerlink" title="布尔常量"></a>布尔常量</h4><p>布尔型常量只有true和false两个值，分别表示真、假两种状态。它们不对应于任何整数值，仅用作逻辑条件的判断。我们在编码时直接使用 true和false 这两个单词，不要加引号。并且注意和C &#x2F; C++语言不同的地方是，布尔型在Java中用关键字 boolean 单词全称，C&#x2F;C++中用 bool 表示布尔型数据</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">boolean</span> i; <span class="comment">//建立布尔类型变量i</span></span><br><span class="line">i=<span class="literal">false</span>;</span><br><span class="line">i=<span class="literal">true</span>; <span class="comment">//变量i的两种状态</span></span><br></pre></td></tr></table></figure>

<h4 id="字符串常量"><a href="#字符串常量" class="headerlink" title="字符串常量"></a>字符串常量</h4><p>字符串常量是用一对双引号括起来的一串字符。它和字符串常量在字符串仅有一个字符的情况下容易混淆：字符串常量包含转义字符 ‘\0’，其实是有两个字符。而字符常量就不一样了，无论字符串含有多少个字符，都是只占一个字符的空间大小</p>
<h4 id="字符常量和null常量"><a href="#字符常量和null常量" class="headerlink" title="字符常量和null常量"></a>字符常量和null常量</h4><p>字符常量就是单个ASCII字符用单引号括起来的形式。eg：’a’、’c’。null常量只有一个值，用null表示，表示对象的引用为空</p>
<h2 id="1-3-运算符与表达式"><a href="#1-3-运算符与表达式" class="headerlink" title="1.3 运算符与表达式"></a>1.3 运算符与表达式</h2><p>在这个部分呢，来谈谈我的感受以及重点的部分，如果你是一名学生，不论是不是主修计算机专业，相信大部分的学校都会开设《C语言》这门课程（顺便问一问，学的怎么样啊？？），课程中的运算符与表达式章节和Java中的类型不能说的上一致，那简直就是完全一样呐</p>
<p>其实和上述内容一样，这些都是属于入门级别的内容。相信我，如果你学过甚至学会了《C语言》这门课程，Java中的这个章节完全可以略过（我是看完感觉没有什么不一样的地方，如有纰漏，实属小的太菜，还望各路大佬告知）</p>
<p>最有用的莫过于运算符的优先级了，这又让我想起了那些“变态”的题目：好多表达式 “ 掺和 ” 在一起，加上各种运算符连接，问你计算结果。仅仅一分的分值不说，最令人头大的是优先级分的也忒明显了，弄一会儿就绕进去了，反正我是很少做对（本人不才）。后来就死记硬背了一些重点的运算符优先级顺序才缓解了这样的窘境</p>
<p>附上一张我背记的表格（图片分页，还望xdm海涵）：</p>
<image src="https://img-blog.csdnimg.cn/17f19b18ad264351a4f7ff98450272ad.png" width="70%">

<h3 id="1-3-1-算术运算符"><a href="#1-3-1-算术运算符" class="headerlink" title="1.3.1 算术运算符"></a>1.3.1 算术运算符</h3><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">TestArithmeticOP</span> &#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span>&#123;</span><br><span class="line">        <span class="type">int</span> n=<span class="number">1859</span>,m;</span><br><span class="line">        <span class="type">int</span> a,b,c,d;</span><br><span class="line">        d=n/<span class="number">1000</span>;</span><br><span class="line">        c=n/<span class="number">100</span>%<span class="number">10</span>;</span><br><span class="line">        b=n/<span class="number">10</span>%<span class="number">10</span>;</span><br><span class="line">        a=n%<span class="number">10</span>;</span><br><span class="line">        m=a*<span class="number">1000</span>+b*<span class="number">100</span>+c*<span class="number">10</span>+d;</span><br><span class="line">        System.out.println(<span class="string">&quot;原数据:n=&quot;</span>+n);</span><br><span class="line">        System.out.println(<span class="string">&quot;逆序数据:m=&quot;</span>+m);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="1-3-2-逻辑运算符"><a href="#1-3-2-逻辑运算符" class="headerlink" title="1.3.2 逻辑运算符"></a>1.3.2 逻辑运算符</h3><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">TestLogicOP</span> &#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span>&#123;</span><br><span class="line">        <span class="type">boolean</span> b1,b2=<span class="literal">true</span>,b3,b4;</span><br><span class="line">        b1=!b2;</span><br><span class="line">        System.out.println(<span class="string">&quot;逻辑值:b1=&quot;</span>+b1+<span class="string">&quot;;&quot;</span>+<span class="string">&quot;b2=&quot;</span>+b2+<span class="string">&quot;;&quot;</span>+<span class="string">&quot;b1&amp;b2=&quot;</span>+(b1&amp;b2));</span><br><span class="line">        <span class="type">int</span> x=<span class="number">2</span>,y=<span class="number">7</span>;</span><br><span class="line">        b3=x&gt;y&amp;&amp;x++==y--; <span class="comment">//逻辑短路问题 x和y的值并未发生改变</span></span><br><span class="line">        System.out.println(<span class="string">&quot;逻辑值:b3=&quot;</span>+b3+<span class="string">&quot;;&quot;</span>+<span class="string">&quot;x=&quot;</span>+x+<span class="string">&quot;;&quot;</span>+<span class="string">&quot;y=&quot;</span>+y);  </span><br><span class="line">        x=<span class="number">2</span>;</span><br><span class="line">        y=<span class="number">7</span>;</span><br><span class="line">        b4=x&gt;y&amp;x++==y--;</span><br><span class="line">        System.out.println(<span class="string">&quot;逻辑值:b4=&quot;</span>+<span class="string">&quot;;&quot;</span>+b4+<span class="string">&quot;x=&quot;</span>+x+<span class="string">&quot;;&quot;</span>+<span class="string">&quot;y=&quot;</span>+y);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">//逻辑与预算符 &amp;&amp; 逻辑或运算符 ||     条件与或  都会发生逻辑短路现象(仅查看一端操作数)</span></span><br><span class="line"></span><br><span class="line"><span class="comment">//&amp; 与  | 或 布尔逻辑与或运算符  两端操作数都看</span></span><br></pre></td></tr></table></figure>
<h3 id="1-3-3-条件运算符"><a href="#1-3-3-条件运算符" class="headerlink" title="1.3.3 条件运算符"></a>1.3.3 条件运算符</h3><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">FindMaxMin</span> &#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span>&#123;</span><br><span class="line">        <span class="type">double</span> d1=<span class="number">1.1</span>,d2=-<span class="number">9.9</span>,d3=<span class="number">96.9</span>;</span><br><span class="line">        <span class="type">double</span> temp,max,min;</span><br><span class="line">        <span class="comment">//Max</span></span><br><span class="line">        temp=d1&gt;d2?d1:d2;</span><br><span class="line">        max=temp&gt;d3?temp:d3;</span><br><span class="line">        <span class="comment">//Min</span></span><br><span class="line">        temp=d1&lt;d2?d1:d2;</span><br><span class="line">        min=temp&lt;d3?temp:d3;</span><br><span class="line">        System.out.println(<span class="string">&quot;max=&quot;</span>+max);</span><br><span class="line">        System.out.println(<span class="string">&quot;min=&quot;</span>+min);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;   <span class="comment">//条件运算符(三目运算符) 使用</span></span><br><span class="line"></span><br><span class="line"><span class="comment">//本题也可以两个变量实现 Max和Min即可 同样使用三目运算符 </span></span><br><span class="line"></span><br><span class="line"><span class="comment">// Max=d1&gt;d2?d1:d2;  Max=d3&gt;Max?d3:Max;</span></span><br></pre></td></tr></table></figure>
<h3 id="1-3-4-位运算符"><a href="#1-3-4-位运算符" class="headerlink" title="1.3.4 位运算符"></a>1.3.4 位运算符</h3><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">TestBitOperation</span> &#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span>&#123;</span><br><span class="line">        <span class="type">byte</span> b1=<span class="number">125</span>,b2=<span class="number">38</span>;</span><br><span class="line">        <span class="type">int</span> i1,i2,i3;</span><br><span class="line">        i1=b1^b2;</span><br><span class="line">        System.out.println(<span class="string">&quot;b1=&quot;</span>+Integer.toString(b1,<span class="number">2</span>));</span><br><span class="line">        System.out.println(<span class="string">&quot;b2=&quot;</span>+Integer.toString(b2,<span class="number">2</span>));</span><br><span class="line">        System.out.println(<span class="string">&quot;异或&quot;</span>+Integer.toString(i1,<span class="number">2</span>));</span><br><span class="line">        i1=i1^b2;</span><br><span class="line">        System.out.println(<span class="string">&quot;(b1^b2)^b2=&quot;</span>+Integer.toString(i1,<span class="number">2</span>));</span><br><span class="line">        i2=b1&gt;&gt;<span class="number">2</span>;</span><br><span class="line">        i3=b1&lt;&lt;<span class="number">2</span>;</span><br><span class="line">        System.out.println(b1+<span class="string">&quot;右移两位等价与除4:&quot;</span>+i2);</span><br><span class="line">        System.out.println(b1+<span class="string">&quot;左移两位等价与乘4:&quot;</span>+i3);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">//逻辑类型运算符区别 &amp; | ^ 根据其两端操作数不同承担不同的功能,当操作数为数值类型时,作为&quot;位运算符&quot;;</span></span><br><span class="line"></span><br><span class="line"><span class="comment">//当操作数为布尔类型时做逻辑运算符 </span></span><br><span class="line"></span><br><span class="line"><span class="comment">// &gt;&gt; 不同于 &gt;&gt;&gt;  前者移位时不包括符号位,后者移位连同符号位一并移位(负数操作完可能会影响其符号)</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// ^ 异或 整型数据时,操作为双行列举 规则&quot;相同为0,不同为1&quot;</span></span><br></pre></td></tr></table></figure>
<h3 id="1-3-5-赋值运算符"><a href="#1-3-5-赋值运算符" class="headerlink" title="1.3.5 赋值运算符"></a>1.3.5 赋值运算符</h3><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">Exchange</span> &#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span>&#123;</span><br><span class="line">        <span class="type">int</span> a=<span class="number">5</span>,b=<span class="number">7</span>;</span><br><span class="line">        System.out.println(<span class="string">&quot;a=&quot;</span>+a+<span class="string">&quot;\tb=&quot;</span>+b);</span><br><span class="line">        a+=b; </span><br><span class="line">        b-=a; </span><br><span class="line">        a-=b; </span><br><span class="line">        System.out.println(<span class="string">&quot;a=&quot;</span>+a+<span class="string">&quot;\tb=&quot;</span>+b);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">//Java中允许连续赋值 z=x=y=2;  在Java中是允许的</span></span><br></pre></td></tr></table></figure>
<h3 id="1-3-6-表达式类型提升"><a href="#1-3-6-表达式类型提升" class="headerlink" title="1.3.6 表达式类型提升"></a>1.3.6 表达式类型提升</h3><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//实现一个三位数各位数字相加</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">Sum</span> &#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span>&#123;</span><br><span class="line">        <span class="type">int</span> n=<span class="number">123</span>,a,b,c,Sum;</span><br><span class="line">        a=n%<span class="number">10</span>;  <span class="comment">//依次 个 十 百位数字分离</span></span><br><span class="line">        b=(n%<span class="number">100</span>)/<span class="number">10</span>;  <span class="comment">//  /的优先级高于%,故加()</span></span><br><span class="line">        c=n/<span class="number">100</span>;</span><br><span class="line">        Sum=a+b+c;</span><br><span class="line">        System.out.println(<span class="string">&quot;AllSum(&quot;</span>+n+<span class="string">&quot;)=&quot;</span>+Sum);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">//对于计算顺序无法确定时,使用()进行区分层级</span></span><br></pre></td></tr></table></figure>

<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//求圆的面积</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">Circle_area</span> &#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span>&#123;</span><br><span class="line">        <span class="keyword">final</span> <span class="type">float</span> PI=<span class="number">3.14f</span>; <span class="comment">//常量</span></span><br><span class="line">        <span class="type">float</span> r=<span class="number">2.5f</span>,area;</span><br><span class="line">        area=PI*r*r;</span><br><span class="line">        System.out.println(<span class="string">&quot;Area(&quot;</span>+r+<span class="string">&quot;)=&quot;</span>+area);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h2 id="1-4-语句及其控制结构"><a href="#1-4-语句及其控制结构" class="headerlink" title="1.4 语句及其控制结构"></a>1.4 语句及其控制结构</h2><h3 id="1-4-1-Java语句"><a href="#1-4-1-Java语句" class="headerlink" title="1.4.1 Java语句"></a>1.4.1 Java语句</h3><p>Java中，程序实现各种各样功能的的原理是：使用类构建程序框架，使用方法实现类的功能，在方法中通过不同的语句结构控制程序流程实现，语句部分主要用例子来描述其使用方法</p>
<p>语句是指程序中的一个完整的执行单元，用于对数据进行加工处理、实现复杂功能。它是Java标识符的集合，由关键字、常量、变量、表达式共同组成。可分为说明性语句和操作性语句两种类型：</p>
<p>（1）说明性语句用来说明包和类的引入、类的声明、变量的声明；</p>
<p>（2）操作性语句包含表达式语句、复合语句、选择语句等；</p>
<h4 id="表达式语句"><a href="#表达式语句" class="headerlink" title="表达式语句"></a>表达式语句</h4><p>表达式后接一个分号构成表达式语句，例如：赋值语句、自加自减语句以及方法调用语句等</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">double variable=100 ; //赋值</span><br><span class="line">variable ++ ; //自增</span><br><span class="line">System.out.println(<span class="string">&quot;Welcome to Java&quot;</span>);//方法调用</span><br></pre></td></tr></table></figure>
<h4 id="复合语句"><a href="#复合语句" class="headerlink" title="复合语句"></a>复合语句</h4><p>复合语句又称块（block）语句，由大括号 “｛｝” 括起来的部分称为语句块。执行复合语句时，实际上是执行该复合语句中所有语句。从语法上讲，无论大括号内有多少语句，都只被看成一条简单语句，多用于选择语句和循环语句中</p>
<h3 id="1-4-2-选择语句"><a href="#1-4-2-选择语句" class="headerlink" title="1.4.2 选择语句"></a>1.4.2 选择语句</h3><h4 id="if···else语句"><a href="#if···else语句" class="headerlink" title="if···else语句"></a>if···else语句</h4><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">BlockDemo</span> &#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span>&#123;</span><br><span class="line">        <span class="type">boolean</span> condition=<span class="literal">false</span>;</span><br><span class="line">        <span class="keyword">if</span>(condition)&#123;</span><br><span class="line">            System.out.println(<span class="string">&quot;Success execute!&quot;</span>);</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">else</span>&#123;</span><br><span class="line">            System.out.println(<span class="string">&quot;error&quot;</span>);    <span class="comment">//此后包含一个换行符</span></span><br><span class="line">            System.out.println(<span class="string">&quot;Waring&quot;</span>);              <span class="comment">//方法调用语句</span></span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h4 id="switch语句"><a href="#switch语句" class="headerlink" title="switch语句"></a>switch语句</h4><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> java.util.Scanner;</span><br><span class="line"></span><br><span class="line"><span class="comment">//需求:使用switch语句将week转换成对应英文字符串</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">Week</span> &#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span>&#123;</span><br><span class="line">        System.out.println(<span class="string">&quot;!Please Enter numbers in 0-6,it will transfer to corresponding Week!&quot;</span>);</span><br><span class="line">        Scanner input=<span class="keyword">new</span> <span class="title class_">Scanner</span>(System.in);</span><br><span class="line">        <span class="type">int</span> week=input.nextInt();</span><br><span class="line">        <span class="keyword">switch</span> (week) &#123;</span><br><span class="line">            <span class="keyword">case</span> <span class="number">0</span> -&gt; System.out.println(<span class="string">&quot;Sunday&quot;</span>);</span><br><span class="line">            <span class="keyword">case</span> <span class="number">1</span> -&gt; System.out.println(<span class="string">&quot;Monday&quot;</span>);</span><br><span class="line">            <span class="keyword">case</span> <span class="number">2</span> -&gt; System.out.println(<span class="string">&quot;Tuesday&quot;</span>);</span><br><span class="line">            <span class="keyword">case</span> <span class="number">3</span> -&gt; System.out.println(<span class="string">&quot;Wednesday&quot;</span>);</span><br><span class="line">            <span class="keyword">case</span> <span class="number">4</span> -&gt; System.out.println(<span class="string">&quot;Thursday&quot;</span>);</span><br><span class="line">            <span class="keyword">case</span> <span class="number">5</span> -&gt; System.out.println(<span class="string">&quot;Friday&quot;</span>);</span><br><span class="line">            <span class="keyword">case</span> <span class="number">6</span> -&gt; System.out.println(<span class="string">&quot;Saturday&quot;</span>);</span><br><span class="line">            <span class="keyword">default</span> -&gt; System.out.println(<span class="string">&quot;Data error!!!&quot;</span>);</span><br><span class="line">        &#125;        <span class="comment">//Java中增强Switch语句</span></span><br><span class="line">        input.close();</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">//优化目的:实现提示用户数据输入,输出对应日期效果</span></span><br></pre></td></tr></table></figure>

<h3 id="1-4-3-循环语句"><a href="#1-4-3-循环语句" class="headerlink" title="1.4.3 循环语句"></a>1.4.3 循环语句</h3><h4 id="for循环语句"><a href="#for循环语句" class="headerlink" title="for循环语句"></a>for循环语句</h4><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//求出个、十、百、千位数字的四次方的和等于该数本身的所有四位数</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">ForTest</span> &#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span>&#123;</span><br><span class="line">        <span class="type">int</span> a,b,c,d;</span><br><span class="line">        System.out.println(<span class="string">&quot;The Data That Meets The Conditions:&quot;</span>);</span><br><span class="line">        <span class="keyword">for</span>(<span class="type">int</span> n=<span class="number">1000</span>;n&lt;<span class="number">10000</span>;n++)&#123;</span><br><span class="line">            a=n/<span class="number">1000</span>;<span class="comment">//千位数字</span></span><br><span class="line">            b=n/<span class="number">100</span>%<span class="number">10</span>;</span><br><span class="line">            c=n/<span class="number">10</span>%<span class="number">10</span>;</span><br><span class="line">            d=n%<span class="number">10</span>;</span><br><span class="line">            <span class="keyword">if</span>(Math.pow(a,<span class="number">4</span>)+Math.pow(b,<span class="number">4</span>)+Math.pow(c,<span class="number">4</span>)+Math.pow(d,<span class="number">4</span>)==n)&#123; <span class="comment">//条件判断  Java中的次方写法</span></span><br><span class="line">                System.out.println(n);</span><br><span class="line">                System.out.println();</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">//定式套用 顺次剥离各位数字（由高位到低位）</span></span><br><span class="line"></span><br><span class="line"><span class="comment">//a=n/1000;千位数字  b=n/100%10;  c=n/10%10;  d=n%10;</span></span><br></pre></td></tr></table></figure>
<h4 id="while循环语句"><a href="#while循环语句" class="headerlink" title="while循环语句"></a>while循环语句</h4><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//求解 x=5时； x+x^2/2!+...+x^n/n!的值 要求x^n/n!的值&lt;1.0 X 10^-8</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">WhileTest</span> &#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span>&#123;</span><br><span class="line">    <span class="type">int</span> x=<span class="number">5</span>;</span><br><span class="line">    <span class="type">double</span> sum=<span class="number">0</span>,d=<span class="number">1.0</span>;<span class="comment">//因为有除法，故用小数 因为值&lt;1.0 X 10^-8 所以d=1.0初值</span></span><br><span class="line">    <span class="type">int</span> n=<span class="number">1</span>;</span><br><span class="line">    <span class="keyword">while</span>(d&gt;<span class="number">1.e-8</span>)&#123; <span class="comment">//注意计数法书写格式</span></span><br><span class="line">        d*=x;</span><br><span class="line">        d/=n;</span><br><span class="line">        sum+=d;</span><br><span class="line">        n++;</span><br><span class="line">    &#125;</span><br><span class="line">    System.out.println(<span class="string">&quot;x=&quot;</span>+x+<span class="string">&quot;;&quot;</span>+<span class="string">&quot;Sum=&quot;</span>+sum);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">//也可使用do-while实现</span></span><br></pre></td></tr></table></figure>
<h4 id="beak语句"><a href="#beak语句" class="headerlink" title="beak语句"></a>beak语句</h4><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> java.io.* ; <span class="comment">//命令编译器载入java_installation/java/io路径下的所有类</span></span><br><span class="line"></span><br><span class="line"><span class="comment">//输入一个正整数,显示所有&lt;=该数的所有素数</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">BreakTest2</span> &#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> <span class="keyword">throws</span> IOException &#123;</span><br><span class="line">        String str;</span><br><span class="line">        BufferedReader buf;</span><br><span class="line">        <span class="type">int</span> k;</span><br><span class="line">        buf = <span class="keyword">new</span> <span class="title class_">BufferedReader</span>(<span class="keyword">new</span> <span class="title class_">InputStreamReader</span>(System.in));</span><br><span class="line">        System.out.println(<span class="string">&quot;Please enter an integer, and press Enter to find all prime numbers less &quot;</span> +</span><br><span class="line">                <span class="string">&quot;than or equal to the integer:&quot;</span>);</span><br><span class="line">        str = buf.readLine();</span><br><span class="line">        k = Integer.parseInt(str);</span><br><span class="line">        System.out.println(<span class="string">&quot;2 to&quot;</span> + <span class="string">&quot; &quot;</span>+ k +<span class="string">&quot; &quot;</span>+<span class="string">&quot;all prime numbers between:&quot;</span>);</span><br><span class="line">        <span class="keyword">for</span> (<span class="type">int</span> <span class="variable">i</span> <span class="operator">=</span> <span class="number">2</span>; i &lt;= k; i++) &#123;</span><br><span class="line">            <span class="keyword">if</span> (isPrime(i))</span><br><span class="line">                System.out.print(i + <span class="string">&quot;\t&quot;</span>);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">static</span> <span class="type">boolean</span> <span class="title function_">isPrime</span><span class="params">(<span class="type">int</span> n)</span> &#123;    <span class="comment">//方法实现</span></span><br><span class="line">        <span class="type">boolean</span> <span class="variable">b</span> <span class="operator">=</span> <span class="literal">true</span>;</span><br><span class="line">        <span class="keyword">for</span> (<span class="type">int</span> <span class="variable">k</span> <span class="operator">=</span> <span class="number">2</span>; k &lt;= n / <span class="number">2</span>; k++) &#123;</span><br><span class="line">            <span class="keyword">if</span> (n % k == <span class="number">0</span>)&#123;</span><br><span class="line">                b = <span class="literal">false</span>;</span><br><span class="line">                <span class="keyword">break</span>;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> b;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">//Java中，如果给出一个完整的限定名(包名、类名），Java编译器就能快速定位到源代码/类</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// Import语句就是用来提供一个合理的路径，使得编译器可以找到某个类</span></span><br></pre></td></tr></table></figure>
<hr>
<h1 id="总结"><a href="#总结" class="headerlink" title="总结"></a>总结</h1><p>我们在学习新知识的时候，不要总是抱有一步登天的想法，什么东西都想着要一下子掌握。这些看似细小的知识点很有可能会成为开发过程中你忽视的那一个。从每一个很小的例子开始，懵懂到知道，知道到理解，理解到上机，上机到掌握，逐步积累经验。相信自己，一定能学会，不要放弃。也正所谓 “ 不积跬步无以至千里，不积小流无以成江海 ”</p>
<p>Gitee&#x2F;GitHub 上我的练习仓库：<span class="exturl" data-url="aHR0cHM6Ly9naXRlZS5jb20vQ29kZXItSmFzb24=">Gitee地址<i class="fa fa-external-link-alt"></i></span>，<span class="exturl" data-url="aHR0cHM6Ly9naXRodWIuY29tL1BEUEVORw==">GitHub地址<i class="fa fa-external-link-alt"></i></span>，供大家学习参考，希望有所收获。码字不易，后面将会更新Java面向对象部分，如果帮到了您可以收藏学习，不要吝啬您的赞哦，您的支持将是我不断前行的动力！！</p>

    </div>

    
    
    

    <footer class="post-footer">

        </br>
        <div class="official-accounts">
             <a target="_blank" rel="noopener" href="https://cdn.jsdelivr.net/gh/PDPENG/jason-storage/blog-img/Jason%20wechat%20account.png">
                 <img src="/images/official-accounts.png" width="80%">
             </a>
        </div>
          <div class="reward-container">
  <div>Thank you for your support to Jason ~</div>
  <button>
    赞赏
  </button>
  <div class="post-reward">
      <div>
        <img src="/images/wechatpay.png" alt="董哲朋 微信">
        <span>微信</span>
      </div>
      <div>
        <img src="/images/alipay.png" alt="董哲朋 支付宝">
        <span>支付宝</span>
      </div>

  </div>
</div>

          

<div class="post-copyright">
<ul>
  <li class="post-copyright-author">
      <strong>本文作者： </strong>董哲朋
  </li>
  <li class="post-copyright-link">
      <strong>本文链接：</strong>
      <a href="https://pdpeng.github.io/2021/08/04/java-basics/" title="Java 基础 ——入坑必读">https://pdpeng.github.io/2021/08/04/java-basics/</a>
  </li>
  <li class="post-copyright-license">
    <strong>版权声明： </strong>本博客所有文章除特别声明外，均采用 <span class="exturl" data-url="aHR0cHM6Ly9jcmVhdGl2ZWNvbW1vbnMub3JnL2xpY2Vuc2VzL2J5LW5jLXNhLzQuMC8="><i class="fab fa-fw fa-creative-commons"></i>BY-NC-SA</span> 许可协议。转载请注明出处！
  </li>
</ul>
</div>

          <div class="post-tags">
              <a href="/tags/Java/" rel="tag"><i class="fa fa-tag"></i> Java</a>
          </div>

        

          <div class="post-nav">
            <div class="post-nav-item">
                <a href="/2021/08/05/single-cycle-cpu/" rel="prev" title="单周期 CPU 设计与实现">
                  <i class="fa fa-chevron-left"></i> 单周期 CPU 设计与实现
                </a>
            </div>
            <div class="post-nav-item">
                <a href="/2021/08/02/distinguish-git-gitee/" rel="next" title="秒懂 Git 与 Gitee（码云）">
                  秒懂 Git 与 Gitee（码云） <i class="fa fa-chevron-right"></i>
                </a>
            </div>
          </div>
    </footer>
  </article>
</div>






    <div class="comments utterances-container"></div>
</div>
  </main>

  <footer class="footer">
    <div class="footer-inner">
<div class="copyright">
  &copy; 2020 – 
  <span itemprop="copyrightYear">2025</span>
  <span class="with-love">
    <i class="fa fa-code"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">董哲朋


<div class="sitetime" style="display: inline-block;">
<span id="sitetime"></span>
<script language=javascript>
	function siteTime(){
		window.setTimeout("siteTime()", 1000);
		var seconds = 1000;
		var minutes = seconds * 60;
		var hours = minutes * 60;
		var days = hours * 24;
		var years = days * 365;
		var today = new Date();
		var todayYear = today.getFullYear();
		var todayMonth = today.getMonth()+1;
		var todayDate = today.getDate();
		var todayHour = today.getHours();
		var todayMinute = today.getMinutes();
		var todaySecond = today.getSeconds();
		var t1 = Date.UTC(2020,09,23,13,15,00); //北京时间 2021-09-23 13:15:00
		var t2 = Date.UTC(todayYear,todayMonth,todayDate,todayHour,todayMinute,todaySecond);
		var diff = t2-t1;
		var diffYears = Math.floor(diff/years);
		var diffDays = Math.floor((diff/days)-diffYears*365);
		var diffHours = Math.floor((diff-(diffYears*365+diffDays)*days)/hours);
		var diffMinutes = Math.floor((diff-(diffYears*365+diffDays)*days-diffHours*hours)/minutes);
		var diffSeconds = Math.floor((diff-(diffYears*365+diffDays)*days-diffHours*hours-diffMinutes*minutes)/seconds);
		document.getElementById("sitetime").innerHTML=" 本站已安全运行 "+diffYears+" 年 "+diffDays+" 天 "+diffHours+" 小时 "+diffMinutes+" 分钟 "+diffSeconds+" 秒 ";
	}
	siteTime();
</script>
</div>
  </span>
</div>
<div class="wordcount" style="display: inline-block;">
  <span class="post-meta-item">
    <span class="post-meta-item-icon">
      <i class="fa fa-chart-line"></i>
    </span>
      <span>站点总字数：</span>
    <span title="站点总字数">312k</span>
  </span>
  <span class="post-meta-item">
    <span class="post-meta-item-icon">
      <i class="fa fa-coffee"></i>
    </span>
      <span>站点阅读时长 &asymp;</span>
    <span title="站点阅读时长">4:43</span>
  </span>
    <div class="beian" style="display: inline-block;">
        <img src="/images/beian.png" alt=""><span class="exturl" data-url="aHR0cDovL3d3dy5iZWlhbi5nb3YuY24vcG9ydGFsL3JlZ2lzdGVyU3lzdGVtSW5mbz9yZWNvcmRjb2RlPTQxMDU4MTAyMDAwMjQx">豫公网安备 41058102000241号 </span>
        <img src="/images/beian.png" alt=""><span class="exturl" data-url="aHR0cHM6Ly9iZWlhbi5taWl0Lmdvdi5jbg==">豫ICP备2021028259号-1 </span>
    </div>

</div>

    </div>
  </footer>

  
  <script src="https://cdn.jsdelivr.net/npm/animejs@3.2.1/lib/anime.min.js" integrity="sha256-XL2inqUJaslATFnHdJOi9GfQ60on8Wx1C2H8DYiN1xY=" crossorigin="anonymous"></script>
  <script src="https://cdn.jsdelivr.net/npm/@next-theme/pjax@0.5.0/pjax.min.js" integrity="sha256-3NkoLDrmHLTYj7csHIZSr0MHAFTXth7Ua/DDt4MRUAg=" crossorigin="anonymous"></script>
  <script src="https://cdn.jsdelivr.net/npm/jquery@3.6.0/dist/jquery.min.js" integrity="sha256-/xUj+3OJU5yExlq6GSYGSHk7tPXikynS7ogEvDej/m4=" crossorigin="anonymous"></script>
  <script src="https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@3.5.7/dist/jquery.fancybox.min.js" integrity="sha256-yt2kYMy0w8AbtF89WXb2P1rfjcP/HTHLT7097U8Y5b8=" crossorigin="anonymous"></script>
<script src="/js/comments.js"></script><script src="/js/utils.js"></script><script src="/js/motion.js"></script><script src="/js/schemes/muse.js"></script><script src="/js/next-boot.js"></script><script src="/js/pjax.js"></script>

  
<script src="https://cdn.jsdelivr.net/npm/hexo-generator-searchdb@1.4.0/dist/search.js" integrity="sha256-vXZMYLEqsROAXkEw93GGIvaB2ab+QW6w3+1ahD9nXXA=" crossorigin="anonymous"></script>
<script src="/js/third-party/search/local-search.js"></script>



  <script src="/js/third-party/fancybox.js"></script>


  


  <script class="next-config" data-name="leancloud_visitors" type="application/json">{"enable":true,"app_id":"XnAU2LpE7JlHt34KKmw24BoM-MdYXbMMI","app_key":"vphcyLncudT8YkHIgks4F1r2","server_url":null,"security":false}</script>
  <script src="/js/third-party/statistics/lean-analytics.js"></script>


  

  <script class="next-config" data-name="enableMath" type="application/json">false</script><script class="next-config" data-name="mathjax" type="application/json">{"enable":true,"tags":"none","js":{"url":"https://cdn.jsdelivr.net/npm/mathjax@3.2.0/es5/tex-mml-chtml.js","integrity":"sha256-r+3itOMtGGjap0x+10hu6jW/gZCzxHsoKrOd7gyRSGY="}}</script>
<script src="/js/third-party/math/mathjax.js"></script>


<script src="https://cdn.jsdelivr.net/gh/PDPENG/live2d-widget@latest/autoload.js"></script><script class="next-config" data-name="utterances" type="application/json">{"enable":true,"repo":"PDPENG/pdpeng.github.io","issue_term":"pathname","theme":"preferred-color-scheme"}</script>
<script src="/js/third-party/comments/utterances.js"></script>

</body>
</html>
